home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Atari Compendium
/
The Atari Compendium (Toad Computers) (1994).iso
/
files
/
compress
/
arc.zoo
/
arcsq.c
< prev
next >
Wrap
C/C++ Source or Header
|
1988-11-16
|
15KB
|
549 lines
/*
* $Header: arcsq.c,v 1.3 88/07/31 18:53:32 hyc Exp $
*/
/*
* ARC - Archive utility - ARCSQ
*
* Version 3.10, created on 01/30/86 at 20:10:46
*
* (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
*
* By: Thom Henderson
*
* Description: This file contains the routines used to squeeze a file when
* placing it in an archive.
*
* Language: Computer Innovations Optimizing C86
*
* Programming notes: Most of the routines used for the Huffman squeezing
* algorithm were lifted from the SQ program by Dick Greenlaw, as adapted to
* CI-C86 by Robert J. Beilstein.
*/
#include <stdio.h>
#include "arc.h"
/* stuff for Huffman squeezing */
#define TRUE 1
#define FALSE 0
#define ERROR (-1)
#define SPEOF 256 /* special endfile token */
#define NOCHILD (-1) /* marks end of path through tree */
#define NUMVALS 257 /* 256 data values plus SPEOF */
#define NUMNODES (NUMVALS+NUMVALS-1) /* number of nodes */
#define MAXCOUNT (unsigned short) 65535 /* biggest unsigned integer */
/*
* The following array of structures are the nodes of the binary trees. The
* first NUMVALS nodes become the leaves of the final tree and represent the
* values of the data bytes being encoded and the special endfile, SPEOF. The
* remaining nodes become the internal nodes of the final tree.
*/
struct nd { /* shared by unsqueezer */
unsigned short weight; /* number of appearances */
short tdepth; /* length on longest path in tree */
short lchild, rchild; /* indices to next level */
} node[NUMNODES]; /* use large buffer */
static int dctreehd; /* index to head of final tree */
/*
* This is the encoding table: The bit strings have first bit in low bit.
* Note that counts were scaled so code fits unsigned integer.
*/
static int codelen[NUMVALS]; /* number of bits in code */
static unsigned short code[NUMVALS]; /* code itself, right adjusted */
static unsigned short tcode; /* temporary code value */
static long valcount[NUMVALS]; /* actual count of times seen */
/* Variables used by encoding process */
static int curin; /* value currently being encoded */
static int cbitsrem; /* # of code string bits left */
static unsigned short ccode; /* current code right justified */
static void scale(), heap(), adjust(), bld_tree(), init_enc(), put_int();
static int cmptrees(), buildenc(), maxchar();
void
init_sq()
{ /* prepare for scanning pass */
int i; /* node index */
/*
* Initialize all nodes to single element binary trees with zero
* weight and depth.
*/
for (i = 0; i < NUMNODES; ++i) {
node[i].weight = 0;
node[i].tdepth = 0;
node[i].lchild = NOCHILD;
node[i].rchild = NOCHILD;
}
for (i = 0; i < NUMVALS; i++)
valcount[i] = 0;
}
void
scan_sq(c) /* add a byte to the tables */
int c; /* byte to add */
{
unsigned short *wp; /* speeds up weight counting */
/* Build frequency info in tree */
if (c == EOF) /* it's traditional */
c = SPEOF; /* dumb, but traditional */
if (*(wp = &node[c].weight) != MAXCOUNT)
++(*wp); /* bump weight counter */
valcount[c]++; /* bump byte counter */
}
long
pred_sq()
{ /* predict size of squeezed file */
int i;
int btlist[NUMVALS]; /* list of intermediate
* b-trees */
int listlen;/* length of btlist */
unsigned short ceiling;/* limit for scaling */
long size = 0; /* predicted size */
int numnodes; /* # of nodes in simplified tree */
scan_sq(EOF); /* signal end of input */
ceiling = MAXCOUNT;
/* Keep trying to scale and encode */
do {
scale(ceiling);
ceiling /= 2; /* in case we rescale */
/*
* Build list of single node binary trees having leaves for
* the input values with non-zero counts
*/
for (i = listlen = 0; i < NUMVALS; ++i) {
if (node[i].weight != 0) {
node[i].tdepth = 0;
btlist[listlen++] = i;
}
}
/*
* Arrange list of trees into a heap with the entry indexing
* the node with the least weight at the top.
*/
heap(btlist, listlen);
/* Convert the list of trees to a single decoding tree */
bld_tree(btlist, listlen);
/* Initialize the encoding table */
init_enc();
/*
* Try to build encoding table. Fail if any code is > 16 bits
* long.
*/
} while (buildenc(0, dctreehd) == ERROR);
/* Initialize encoding variables */
cbitsrem = 0; /* force initial read */
curin = 0; /* anything but endfile */
for (i = 0; i < NUMVALS; i++) /* add bits for each code */
size += valcount[i] * codelen[i];
size = (size + 7) / 8; /* reduce to number of bytes */
numnodes = dctreehd < NUMVALS ? 0 : dctreehd - (NUMVALS - 1);
size += sizeof(short) + 2 * numnodes * sizeof(short);
return size;
}
/*
* The count of number of occurrances of each input value have already been
* prevented from exceeding MAXCOUNT. Now we must scale them so that their
* sum doesn't exceed ceiling and yet no non-zero count can become zero. This
* scaling prevents errors in the weights of the interior nodes of the
* Huffman tree and also ensures that the codes will fit in an unsigned
* integer. Rescaling is used if necessary to limit the code length.
*/
static void
scale(ceil)
unsigned short ceil; /* upper limit on total weight */
{
register int i;
int ovflw, divisor;
unsigned short w, sum;
unsigned char increased; /* flag */
do {
for (i = sum = ovflw = 0; i < NUMVALS; ++i) {
if (node[i].weight > (ceil - sum))
++ovflw;
sum += node[i].weight;
}
divisor = ovflw + 1;
/* Ensure no non-zero values are lost */
increased = FALSE;
for (i = 0; i < NUMVALS; ++i) {
w = node[i].weight;
if (w < divisor && w != 0) { /* Don't fail to provide
* a code if it's used
* at all */
node[i].weight = divisor;
increased = TRUE;
}
}
} while (increased);
/* Scaling factor chosen, now scale */
if (divisor > 1)
for (i = 0; i < NUMVALS; ++i)
node[i].weight /= divisor;
}
/*
* heap() and adjust() maintain a list of binary trees as a heap with the top
* indexing the binary tree on the list which has the least weight or, in
* case of equal weights, least depth in its longest path. The depth part is
* not strictly necessary, but tends to avoid long codes which might provoke
* rescaling.
*/
static void
heap(list, length)
int list[], length;
{
register int i;
for (i = (length - 2) / 2; i >= 0; --i)
adjust(list, i, length - 1);
}
/* Make a heap from a heap with a new top */
static void
adjust(list, top, bottom)
int list[], top, bottom;
{
register int k, temp;
k = 2 * top + 1; /* left child of top */
temp = list[top]; /* remember root node of top tree */
if (k <= bottom) {
if (k < bottom && cmptrees(list[k], list[k + 1]))
++k;
/* k indexes "smaller" child (in heap of trees) of top */
/* now make top index "smaller" of old top and smallest child */
if (cmptrees(temp, list[k])) {
list[top] = list[k];
list[k] = temp;
/* Make the changed list a heap */
adjust(list, k, bottom); /* recursive */
}
}
}
/*
* Compare two trees, if a > b return true, else return false. Note
* comparison rules in previous comments.
*/
static int
cmptrees(a, b)
int a, b; /* root nodes of trees */
{
if (node[a].weight > node[b].weight)
return TRUE;
if (node[a].weight == node[b].weight)
if (node[a].tdepth > node[b].tdepth)
return TRUE;
return FALSE;
}
/*
* HUFFMAN ALGORITHM: develops the single element trees into a single binary
* tree by forming subtrees rooted in interior nodes having weights equal to
* the sum of weights of all their descendents and having depth counts
* indicating the depth of their longest paths.
*
* When all trees have been formed into a single tree satisfying the heap
* property (on weight, with depth as a tie breaker) then the binary code
* assigned to a leaf (value to be encoded) is then the series of left (0)
* and right (1) paths leading from the root to the leaf. Note that trees are
* removed from the heaped list by moving the last element over the top
* element and reheapin